package com.ly.web.controller.erp;

import java.util.List;

import com.ly.common.PageParam;
import com.ly.common.utils.StringUtils;
import com.ly.heart.domain.dto.KingInvoicePayableDto;
import com.ly.heart.domain.vo.KingInvoicePayableMenuVo;
import com.ly.heart.service.IKingInvoicePayableService;
import com.ly.heart.service.king.PayablePushService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ly.common.annotation.Log;
import com.ly.common.core.controller.BaseController;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.enums.BusinessType;
import com.ly.heart.domain.KingInvoicePayableEntity;
import com.ly.common.utils.poi.ExcelUtil;

import javax.annotation.Resource;

/**
 * 金蝶-发票-应付单/采购开票Controller
 *
 * @author wutao
 * @date 2024-06-26
 */
@Api ( value = "KingInvoicePayable", tags = "金蝶-发票-应付单/采购开票" )
@RestController
@RequestMapping ( "/heart/KingInvoicePayable" )
public class KingInvoicePayableController extends BaseController {

    @Resource
    private IKingInvoicePayableService kingInvoicePayableEntityService;
    @Resource
    private PayablePushService payablePushService;

    /**
     * 查询金蝶-发票-应付单/采购开票列表
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingInvoicePayable:list')" )
    @GetMapping ( "/list" )
    @ApiOperation ( "查询金蝶-发票-应付单/采购开票列表" )
    @Log ( title = "采购开票(应付单)列表" )
    public AjaxResult list ( KingInvoicePayableEntity kingInvoicePayableEntity ) {
        List<KingInvoicePayableEntity> list = kingInvoicePayableEntityService.selectKingInvoicePayableEntityList ( kingInvoicePayableEntity );
        PageParam p = new PageParam ();
        p.setRecords ( list ).setTotal ( kingInvoicePayableEntityService.queryCount ( kingInvoicePayableEntity ) );
        return AjaxResult.success ( p );
    }

    @PostMapping ( "/queryList" )
    @ApiOperation ( "付款单选单，选择应付单" )
    @Log ( title = "付款单选单，选择应付单" )
    public AjaxResult queryList ( @RequestBody KingInvoicePayableEntity kingInvoicePayableEntity ) {
        List<KingInvoicePayableMenuVo> list = kingInvoicePayableEntityService.queryList ( kingInvoicePayableEntity );
        Integer integer = kingInvoicePayableEntityService.queryCountByMenu ( kingInvoicePayableEntity );
        PageParam p = new PageParam ();
        p.setRecords ( list ).setTotal ( integer );
        return AjaxResult.success ( p );
    }

    @PostMapping ( "/queryListByPayable" )
    @ApiOperation ( "应付单冲红，选择应付单" )
    @Log ( title = "应付单冲红，选择应付单" )
    public AjaxResult queryListByPayable ( @RequestBody KingInvoicePayableEntity kingInvoicePayableEntity ) {
        return kingInvoicePayableEntityService.queryListByPayable ( kingInvoicePayableEntity );
    }

    /**
     * 导出金蝶-发票-应付单/采购开票列表
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingInvoicePayable:export')" )
    @Log ( title = "金蝶-发票-应付单/采购开票", businessType = BusinessType.EXPORT )
    @GetMapping ( "/export" )
    @ApiOperation ( "导出金蝶-发票-应付单/采购开票列表" )
    public AjaxResult export ( KingInvoicePayableEntity kingInvoicePayableEntity ) {
        List<KingInvoicePayableEntity> list = kingInvoicePayableEntityService.selectKingInvoicePayableEntityList ( kingInvoicePayableEntity );
        ExcelUtil<KingInvoicePayableEntity> util = new ExcelUtil<KingInvoicePayableEntity> ( KingInvoicePayableEntity.class );
        return util.exportExcel ( list, "info" );
    }

    /**
     * 获取金蝶-发票-应付单/采购开票详细信息
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingInvoicePayable:query')" )
    @GetMapping ( value = "/{id}" )
    @ApiOperation ( "获取金蝶-发票-应付单/采购开票详细信息" )
    public AjaxResult getInfo ( @PathVariable ( "id" ) String id ) {
        return AjaxResult.success ( kingInvoicePayableEntityService.selectKingInvoicePayableEntityById ( id ) );
    }

    /**
     * 暂存单据，如果已存在的单据不可暂存
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingInvoicePayable:add')" )
    @Log ( title = "暂存应付单/采购开票", businessType = BusinessType.INSERT )
    @PostMapping ( "draft" )
    @ApiOperation ( "暂存应付单/采购开票" )
    public AjaxResult draft ( @RequestBody KingInvoicePayableDto kingInvoicePayableDto ) {
        if ( StringUtils.isNotBlank ( kingInvoicePayableDto.getKingInvoicePayableEntity ().getId () ) ) {
            return kingInvoicePayableEntityService.updateKingInvoicePayableEntity ( kingInvoicePayableDto );
        }
        return kingInvoicePayableEntityService.draft ( kingInvoicePayableDto );
    }

    /**
     * 提交单据，分两种情况：
     * 1、直接提交，表示录入数据后直接将所有数据提交到系统
     * 2、暂存后的提交，表示在列表页勾选一条记录或在修改页面，提交一条已存在的且状态为暂存的单据，需要判断是否有Id，如果有则走修改，否额新增
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingInvoicePayable:add')" )
    @Log ( title = "金蝶-发票-应付单/采购开票", businessType = BusinessType.INSERT )
    @PostMapping ( "submit" )
    @ApiOperation ( "新增金蝶-发票-应付单/采购开票" )
    public AjaxResult submit ( @RequestBody KingInvoicePayableEntity payableEntity ) {
        return kingInvoicePayableEntityService.submit ( payableEntity );
    }

    /**
     * 修改金蝶-发票-应付单/采购开票
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingInvoicePayable:edit')" )
    @Log ( title = "金蝶-发票-应付单/采购开票", businessType = BusinessType.UPDATE )
    @PutMapping
    @ApiOperation ( "修改金蝶-发票-应付单/采购开票" )
    public AjaxResult edit ( @RequestBody KingInvoicePayableDto kingInvoicePayableDto ) {
        return kingInvoicePayableEntityService.updateKingInvoicePayableEntity ( kingInvoicePayableDto );
    }

    /**
     * 删除金蝶-发票-应付单/采购开票
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingInvoicePayable:remove')" )
    @Log ( title = "金蝶-发票-应付单/采购开票", businessType = BusinessType.DELETE )
    @DeleteMapping ( "/{ids}" )
    @ApiOperation ( "删除金蝶-发票-应付单/采购开票" )
    public AjaxResult remove ( @PathVariable String[] ids ) {
        return kingInvoicePayableEntityService.deleteKingInvoicePayableEntityByIds ( ids );
    }

    @Log ( title = "推送采购开票/应付单到金蝶" )
    @GetMapping ( "/send/{id}" )
    @ApiOperation ( "推送销售开票/应收单到金蝶" )
    public AjaxResult send ( @PathVariable String id ) {
        KingInvoicePayableEntity payableEntity = new KingInvoicePayableEntity ();
        payableEntity.setId ( id );
        payablePushService.push ( payableEntity );
        return AjaxResult.success ( "推送成功" );
    }

}
